local assigned = redis.call("hget", "leastCountAssignMap", KEYS[1])
if assigned == false then
    local card = redis.call("zcard", "leastCountMap")
    local incr = redis.call("zscore", "keyByStat_count", KEYS[1])
    if incr == false then
        incr = 1
    end
    if card < tonumber(KEYS[2]) then
        redis.call("zadd", "leastCountMap", incr, card)
        redis.call("hset", "leastCountAssignMap", KEYS[1], card)
        return card
    else
        local min = redis.call("zrange", "leastCountMap", 0, 0)
        redis.call("zincrby", "leastCountMap", incr, min[1])
        redis.call("hset", "leastCountAssignMap", KEYS[1], min[1])
        return tonumber(min[1])
    end
else
    return tonumber(assigned)
end
